define(function () {
    var self;
    var TV = function () {
        self = this;
        self.on('key.up', self.moveToNextUpFocus);
        self.on('key.down', self.moveToNextDownFocus);
        self.on('key.left', self.moveToNextLeftFocus);
        self.on('key.right', self.moveToNextRightFocus);

    };

    TV.prototype = {
        modules:[],
        components: [],
        page:{},
        curFocus: '',
        focusStack: [],
        focusStackLength: 10,
        handlers: [],
        listeners: [],
        nextFocusDown: '',
        nextFocusUp: '',
        nextFocusLeft: '',
        nextFocusRight: '',
        blur: function (fn) {
            self.on('key.up', fn);
            self.on('key.down', fn);
            self.on('key.left', fn);
            self.on('key.right', fn);
        },
        focus: function (fn) {
            self.on('key.up', fn);
            self.on('key.down', fn);
            self.on('key.left', fn);
            self.on('key.right', fn);
        },
        click: function () {

        },
        use:function (component) {
            self.components[component.id]=component;
        },
        moveToNextFocus: function (focusId) {
            // console.log(self.components[self.curFocus],self.curFocus)
            if (self.curFocus)
                self.components[self.curFocus].onBlur();
            if (typeof self.components[focusId] !== 'undefined') {
                self.curFocus = focusId;
                self.nextFocusUp = self.components[focusId].nextFocusUp;
                self.nextFocusDown = self.components[focusId].nextFocusDown;
                self.nextFocusLeft = self.components[focusId].nextFocusLeft;
                self.nextFocusRight = self.components[focusId].nextFocusRight;
            }
            self.components[self.curFocus].onFocus();
            console.log(self.components[self.curFocus])
        },
        moveToNextUpFocus: function () {
            self.moveToNextFocus(self.nextFocusUp)
        },
        moveToNextDownFocus: function () {
            self.moveToNextFocus(self.nextFocusDown)
        },
        moveToNextLeftFocus: function () {
            self.moveToNextFocus(self.nextFocusLeft)
        },
        moveToNextRightFocus: function () {
            self.moveToNextFocus(self.nextFocusRight)
        },
        on: function (type, handler) {
            if (typeof self.handlers[type] === 'undefined') {
                self.handlers[type] = [];
            }
            self.listeners.push(type);
            self.handlers[type].push(handler);
            return self;
        },
        off: function (type, handler) {
            if (self.handlers[type] instanceof Array) {
                var handlers = self.handlers[type];
                for (var i = 0, len = handlers.length; i < len; i++) {
                    if (handlers[i] === handler) {
                        break;
                    }
                }
                self.listeners.splice(i, 1);
                handlers.splice(i, 1);
                return self;
            }
        },
        emit: function (event) {
            if (!event.target) {
                event.target = self;
            }
            if (self.handlers[event.type] instanceof Array) {
                var handlers = self.handlers[event.type];
                for (var i = 0, len = handlers.length; i < len; i++) {
                    handlers[i](event);
                    return true;
                }
            }
            return false;
        },
        _init: function () {
        }
    };
    return new TV;
});